package cn.com.xy.sms.sdk.ui.popu.widget;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil.OnBottomClick;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class MoreButtonDataDialog {
    private JSONArray train_array;
    private Context mContext;
    private int mCurrentTrainIndex;
    private List<View> mAllSelects;
    private OnBottomClick mOnBottomClick = null;
    private AlertDialog mAlertDialog;
    public static final int CONFIRM = 0;
    public static final int CANNEL = 1;
    public DialogParams params;

    private static final String MAINTEXT = "main_text";
    private static final String SECTEXT = "sec_text";
    private static final String TITLETEXT = "title_text";

    public MoreButtonDataDialog(JSONArray train_array, Context context, int currentTrainIndex) {
        super();
        this.train_array = train_array;
        this.mContext = context;
        this.mCurrentTrainIndex = currentTrainIndex;
        mAllSelects = new ArrayList<View>();

        params = new DialogParams();
        params.mDefaultTextColor = R.color.duoqu_ui_3010;
        params.mSelectTextColor = R.color.duoqu_ui_3010;
        params.mDefaultTitleName = "";
    }

    @SuppressLint("ResourceAsColor")
    public void ShowDialog(OnBottomClick click) {
        try {
            this.mOnBottomClick = click;
            if (train_array != null && train_array.length() > 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle(train_array.getJSONObject(0).optString(TITLETEXT));
                builder.setNegativeButton(mContext.getString(R.string.duoqu_ui_cancel),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (mOnBottomClick != null) {
                                    mOnBottomClick.Onclick(CANNEL, mCurrentTrainIndex);
                                }
                            }
                        });

                View customView = LayoutInflater.from(mContext).inflate(R.layout.duoqu_ui_select_list_dialog, null);
                LinearLayout rootLayout = (LinearLayout) customView.findViewById(R.id.item_roots);

                for (int position = 0; position < train_array.length(); position++) {
                    try {
                        JSONObject itemJson = train_array.optJSONObject(position);

                        View itemChildView = View.inflate(mContext, R.layout.duoqu_ui_list_items_content_part, null);

                        TextView itemCity = (TextView) itemChildView.findViewById(R.id.item_city);
                        LinearLayout itemLayout = (LinearLayout) itemChildView.findViewById(R.id.item_layout);
                        String secText = itemJson.optString(SECTEXT);
                        DisplayMetrics metrics = ContentUtil.getDisplayMetrics();
                        ViewGroup.LayoutParams layoutParams = itemLayout.getLayoutParams();
                        if (StringUtils.isNull(secText)) {
                            layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48,
                                    metrics);
                            itemCity.setVisibility(View.GONE);
                        } else {
                            layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60,
                                    metrics);
                            itemCity.setText(secText);
                            itemCity.setVisibility(View.VISIBLE);
                        }
                        itemLayout.setLayoutParams(layoutParams);

                        TextView itemText = (TextView) itemChildView.findViewById(R.id.item_text);
                        itemText.setText(itemJson.optString(MAINTEXT));

                        itemChildView.setOnClickListener(new OnItemSelectDialog());
                        mAllSelects.add(itemChildView);
                        rootLayout.addView(itemChildView);
                        
                        if(position != train_array.length() - 1){
                            View spliteView = new View(mContext);
                            spliteView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
                            spliteView.setBackgroundResource(R.color.item_splite_color);
                            rootLayout.addView(spliteView);
                        }
                    } catch (Throwable e) {
                    }
                }
                builder.setView(customView);
                mAlertDialog = builder.create();
                mAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                mAlertDialog.show();
                Window window = mAlertDialog.getWindow();
                window.setGravity(Gravity.CENTER);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(lp);
            }
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("AirSelectDialog", e);
        }
    }

    private class OnItemSelectDialog implements OnClickListener {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < mAllSelects.size(); i++) {
                View item = mAllSelects.get(i);
                if (item == v) {
                    mCurrentTrainIndex = i;
                    mAlertDialog.dismiss();
                    if (mOnBottomClick != null) {
                        mOnBottomClick.Onclick(CONFIRM, mCurrentTrainIndex);
                    }
                    
                    break;
                }
            }
        }
    }

    public class DialogParams {
        public int mDefaultTextColor;
        public int mSelectTextColor;
        public String mDefaultTitleName;
        public String mSelectItemKey;
    }

}
